package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.provider.mapper.GoodsPointMapper;
import com.weixing.mall.provider.model.domain.GoodsPoint;
import com.weixing.mall.provider.model.dto.GoodsPointDto;
import com.weixing.mall.provider.model.query.GoodsPointQuery;
import com.weixing.mall.provider.model.vo.GoodsPointVo;
import com.weixing.mall.provider.service.IGoodsPointService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 积分商品表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2020-05-08
 */
@Service
public class GoodsPointServiceImpl extends ServiceImpl<GoodsPointMapper, GoodsPoint> implements IGoodsPointService {
    @Autowired
    private GoodsPointMapper goodsPointMapper;

    @Override
    public IPage<GoodsPointVo> selectGoodsPointList(Page page,GoodsPointQuery goodsPointQuery) {
        // page(page,new LambdaQueryWrapper<GoodsPoint>().orderByDesc(GoodsPoint::getCreatedTime));
        return goodsPointMapper.selectGoodsPointList(page,goodsPointQuery);
    }

    @Override
    public Result saveOrUpdate(GoodsPointDto goodsPointDto, Long operatorId) {
        if(ObjectUtil.isEmpty(goodsPointDto)){
            return ResultUtil.error("failed");
        }
        GoodsPoint goodsPoint = new GoodsPoint();
        BeanUtils.copyProperties(goodsPointDto,goodsPoint);
        if(goodsPointDto.getId() == null){
            goodsPoint.setCreatedBy(operatorId);
        }else{
            goodsPoint.setUpdatedBy(operatorId);
        }
        return saveOrUpdate(goodsPoint)?ResultUtil.success("success"):ResultUtil.error("failed");
    }
}
